package com.authine.cloudpivot.ext.dto;

import com.authine.cloudpivot.ext.enums.order.BearTypeEnum;
import com.authine.cloudpivot.ext.enums.order.StockBillMaterialTypeEnum;
import com.authine.cloudpivot.ext.utils.BigDecimalUtils;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName StockBillMaterialDto
 * @Description: 库存相关单据物料表
 * @Author cjj
 * @Date 2021/4/9
 * @Version V1.0
 **/
@Data
@Schema(description = "StockBillMaterialDto")
@ApiModel("库存相关单据物料表")
public class StockBillMaterialDto {
    @Schema(description = "库存相关单据物料ID")
    private String id;
    @Schema(description = "库存相关单据物料ID集合")
    private List<String> ids;
    @Schema(description = "类型集合")
    private List<StockBillMaterialTypeEnum> typeList;
    @Schema(description = "客户id")
    private SelectorFormat customerId;//客户id
    @Schema(description = "供应商id")
    private SelectorFormat vendorId;//供应商id
    @Schema(description = "用户id")
    private SelectorFormat memberId;//用户id
    @Schema(description = "商品id")
    private String goodsId;//商品id
    @Schema(description = "来源id")
    private String srcId;//来源id
    @Schema(description = "单据id")
    private String stockBillId;//单据id
    @Schema(description = "类型")
    private String type;
    @Schema(description = "品牌")
    private String brand;
    @Schema(description = "商品名称")
    private String name;
    @Schema(description = "型号")
    private String model;
    @Schema(description = "规格")
    private String spec;
    @Schema(description = "单位")
    private String unit;
    @Schema(description = "原始数量")
    private BigDecimal orginalNum;
    @Schema(description = "数量")
    private BigDecimal num;
    @Schema(description = "入库数量")
    private BigDecimal receiveNum;
    @Schema(description = "领用数量")
    private BigDecimal useNum;
    @Schema(description = "退库数量")
    private BigDecimal returnNum;
    @Schema(description = "盘点数量")
    private BigDecimal checkNum;
    @Schema(description = "税率")
    private BigDecimal taxRate; // 商品税率 --
    @Schema(description = "未税单价")
    private BigDecimal pretaxPrice; // 未税单价 --
    @Schema(description = "含税单价")
    private BigDecimal price; // 含税单价 *
    @Schema(description = "未税合计")
    private BigDecimal pretaxTotal; // 未税合计 --
    @Schema(description = "含税合计")
    private BigDecimal total; // 含税合计
    @Schema(description = "税额")
    private BigDecimal taxPayable; // 税额
    @Schema(description = "是否可结算")
    private Boolean settlement; // 是否可结算
    @Schema(description = "承担类型")
    private BearTypeEnum bearTypeEnum; // 承担类型
    @Schema(description = "承担类型")
    private String bearType; // 承担类型
    @Schema(description = "开始时间", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timeBegin;
    @Schema(description = "结束时间", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timeEnd;
    @Schema(description = "客户名称")
    private String customerName;
    @Schema(description = "供应商名称")
    private String vendorName;
    @Schema(description = "下单日期-开始日期", pattern = "yyyy-MM-dd HH:mm:ss")
    private String startTime;
    @Schema(description = "下单日期-结束日期", pattern = "yyyy-MM-dd HH:mm:ss")
    private String endTime;
    @Schema(description = "pageIndex")
    private Integer pageIndex;
    @Schema(description = "pageSize")
    private Integer pageSize;

    public StockBillMaterialDto() {
    }

    public static Map<String, Object> voToEntity(StockBillMaterialDto stockBillMaterialVO) {

        BigDecimal receiveNum = BigDecimal.ZERO;
        BigDecimal useNum = BigDecimal.ZERO;
        BigDecimal returnNum = BigDecimal.ZERO;
        BigDecimal num = stockBillMaterialVO.getNum();

        //默认平台承担责任
        if(Objects.isNull(stockBillMaterialVO.getBearType())){
            stockBillMaterialVO.setBearType(BearTypeEnum.CUSTOMER_BEAR.name());
        }

        if (StockBillMaterialTypeEnum.RECEIVE.name().equals(stockBillMaterialVO.getType())) {
            receiveNum = stockBillMaterialVO.getReceiveNum();
        } else if (StockBillMaterialTypeEnum.USE.name().equals(stockBillMaterialVO.getType())) {
            useNum = stockBillMaterialVO.getUseNum();
        } else if (StockBillMaterialTypeEnum.RETURN.name().equals(stockBillMaterialVO.getType())) {
            returnNum = stockBillMaterialVO.getReturnNum();
        } else if (StockBillMaterialTypeEnum.CHECK.name().equals(stockBillMaterialVO.getType())) {
            if (stockBillMaterialVO.getNum().compareTo(BigDecimalUtils.isNull(stockBillMaterialVO.getOrginalNum()))==1) {
                stockBillMaterialVO.setType(StockBillMaterialTypeEnum.CHECK_PROFIT.name());
                if ("".equals(stockBillMaterialVO.getBearType())) throw new RuntimeException("请选择责任方");
            } else if (stockBillMaterialVO.getNum().compareTo(BigDecimalUtils.isNull(stockBillMaterialVO.getOrginalNum())) ==-1) {
                stockBillMaterialVO.setType(StockBillMaterialTypeEnum.CHECK_LOSS.name());
                if ("".equals(stockBillMaterialVO.getBearType())) throw new RuntimeException("请选择责任方");
            }
            num = BigDecimalUtils.sub(stockBillMaterialVO.getOrginalNum(), stockBillMaterialVO.getNum());
        }

        Map<String, Object> stockBillMaterialMap= new HashMap<>();
        stockBillMaterialMap.put("src_id", stockBillMaterialVO.getId());
        stockBillMaterialMap.put("stock_bill_id", stockBillMaterialVO.getStockBillId());
        stockBillMaterialMap.put("goods_id", stockBillMaterialVO.getGoodsId());
        stockBillMaterialMap.put("src_id", stockBillMaterialVO.getSrcId());
        stockBillMaterialMap.put("type", stockBillMaterialVO.getType());
        stockBillMaterialMap.put("brand", stockBillMaterialVO.getBrand());
        stockBillMaterialMap.put("name", stockBillMaterialVO.getName());
        stockBillMaterialMap.put("model", stockBillMaterialVO.getModel());
        stockBillMaterialMap.put("spec", stockBillMaterialVO.getSpec());
        stockBillMaterialMap.put("unit", stockBillMaterialVO.getUnit());
        stockBillMaterialMap.put("orginal_num", stockBillMaterialVO.getOrginalNum());
        stockBillMaterialMap.put("num", num);
        stockBillMaterialMap.put("receive_num", receiveNum);
        stockBillMaterialMap.put("use_num", useNum);
        stockBillMaterialMap.put("return_num", returnNum);
        stockBillMaterialMap.put("check_num", stockBillMaterialVO.getCheckNum());
        stockBillMaterialMap.put("tax_rate", stockBillMaterialVO.getTaxRate());
        stockBillMaterialMap.put("pretax_price", BigDecimalUtils.isNull(stockBillMaterialVO.getPretaxPrice()));
        stockBillMaterialMap.put("price", BigDecimalUtils.isNull(stockBillMaterialVO.getPrice()));
        stockBillMaterialMap.put("total", BigDecimalUtils.isNull(stockBillMaterialVO.getTotal()));
        stockBillMaterialMap.put("pretax_total", BigDecimalUtils.isNull(stockBillMaterialVO.getPretaxTotal()));
        stockBillMaterialMap.put("tax_payable", BigDecimalUtils.isNull(stockBillMaterialVO.getTaxPayable()));
        stockBillMaterialMap.put("settlement", true);
        stockBillMaterialMap.put("bear_type", stockBillMaterialVO.getBearType());
        return stockBillMaterialMap;
    }

    public StockBillMaterialDto(SelectorFormat vendorId, String goodsId, BigDecimal num) {
        this.vendorId = vendorId;
        this.goodsId = goodsId;
        this.num = num;
    }
}
